package com.zoe.onelink.business.util.ws.message;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.zoe.onelink.business.constant.RequestConst;
import com.zoe.onelink.business.util.DateUtils;
import com.zoe.onelink.business.util.XmlUtil;
import com.zoe.onelink.util.OnelinkContextHolder;
import com.zoe.onelink.util.IdUtil;

import java.util.Date;

/**
 * <p>标题: 请求消息头</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2016</p>
 * <p>公司: 智业软件股份有限公司</p>
 * @version: 1.0
 * @author: cxy
 * @date 2016/12/21
 */
@XStreamAlias("msgHeader")
public class Header {

    private String msgNo;//消息唯一标识:GUID
    private String format;//“XML”、 “HL7”
    private String sendSystem;//消息发送方(系统/模块）
    private String sender;//消息发送人
    private String sendTime;//消息发送时间，YYYY-MM-DD HH:MM:SS
    private Integer sendCount;//消息发送次数：（0，1，2…..）0--首次,1--第一次重发
    private String versionId;//消息版本号，用于兼容多个版本消息格式

    public Header() {
        this.msgNo = IdUtil.uuid();
        this.format = "XML";
        this.sendTime = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        this.sendCount = 1;
        this.versionId = "1.0";
        String districtCode = OnelinkContextHolder.getString(RequestConst.HEADER_DISTRICT_CODE);
        if (districtCode == null) {
            districtCode = "";
        }
        this.sendSystem = districtCode;
        this.sender = OnelinkContextHolder.getString(RequestConst.HEADER_OPERATOR);
    }

    public Header(String msgNo, String format, String sendSystem, String sender, String sendTime, Integer
            sendCount, String versionId) {
        this.msgNo = msgNo;
        this.format = format;
        this.sendSystem = sendSystem;
        this.sender = sender;
        this.sendTime = sendTime;
        this.sendCount = sendCount;
        this.versionId = versionId;
    }

    public String getMsgNo() {
        return msgNo;
    }

    public void setMsgNo(String msgNo) {
        this.msgNo = msgNo;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getSendSystem() {
        return sendSystem;
    }

    public void setSendSystem(String sendSystem) {
        this.sendSystem = sendSystem;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getSendTime() {
        return sendTime;
    }

    public void setSendTime(String sendTime) {
        this.sendTime = sendTime;
    }

    public Integer getSendCount() {
        return sendCount;
    }

    public void setSendCount(Integer sendCount) {
        this.sendCount = sendCount;
    }

    public String getVersionId() {
        return versionId;
    }

    public void setVersionId(String versionId) {
        this.versionId = versionId;
    }

    public String getXml() {
        try {
            return XmlUtil.toXml(this);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        Header header = new Header();
        header.getXml();
        System.out.println(header.getXml());
    }
}
